package com.ymtc.ibm.code;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.ibm.mq.*;
import com.ibm.mq.constants.MQConstants;

public class MQClientPut {
	private String hostname = "zchengz";
	private int port = 1414; 
	private String channel = "CHL.SVRCONN"; 
	private String qManager = "QM_TEST"; 
	private MQQueueManager qMgr; 
	private static String qName = "IN.Q";
	private MQQueue queue;

	public static void main(String[] args) {		
		MQClientPut mqc = new MQClientPut();
		mqc.runPutMsg();
	}

	public void MQStart() {
		try {			
			MQEnvironment.hostname = hostname; 
			MQEnvironment.channel = channel; 
			MQEnvironment.port = port;
			MQEnvironment.CCSID = 1208;
			//MQEnvironment.userID = "db2admin";
			//MQEnvironment.password = "db2admin";
			
			/*
			String Userid = "zcheng";
			String Password = "zxcv0bnm";
			Hashtable<String, Object> h = new Hashtable<String, Object>();		
			h.put(MQConstants.USER_ID_PROPERTY, Userid);
			h.put(MQConstants.PASSWORD_PROPERTY, Password);
			h.put(MQConstants.USE_MQCSP_AUTHENTICATION_PROPERTY, true);
			*/
			//MQEnvironment.properties.put(MQConstants.TRANSPORT_PROPERTY,MQConstants.TRANSPORT_MQSERIES);
			// using client-connection channel
			//java.net.URL chanTab = new java.net.URL("file:///C:/temp/AMQCLCHL.TAB");
			//qMgr = new MQQueueManager(qManager, chanTab);
			qMgr = new MQQueueManager(qManager);
			//int openOptions = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_OUTPUT ;
			int openOptions = MQConstants.MQOO_OUTPUT | MQConstants.MQOO_FAIL_IF_QUIESCING | MQConstants.MQOO_BIND_NOT_FIXED ;
			queue = qMgr.accessQueue(qName, openOptions);
			System.out.println(">>> Open Queue successful!");
		} catch (Exception ex) {
			ex.printStackTrace(System.out);
		}
	}

	public void MQStop() {
		try {
			queue.close();
			qMgr.disconnect();
		} catch (MQException ex) {
			System.out.println("stop err!!!!");
		}
	}

	public void sendMsgToQueue() {
		try {
			MQPutMessageOptions pmo = new MQPutMessageOptions();
			pmo.options = pmo.options + MQConstants.MQPMO_LOGICAL_ORDER;

			MQMessage msg = new MQMessage();
			System.out.println(">>> MQMD version is : " + msg.getVersion());
			//msg.characterSet = 819;
			
			int number=1;
			int groupNum=3;
			byte[] buffer = null;
			
			while (number <= groupNum) {
				/*if (number ==1) {
					msg.persistence = MQConstants.MQPER_PERSISTENT;
					msg.writeString("<Manifest><Head_Info><SenderID>0906000000001</SenderID><ReceiverID>0908</ReceiverID></Head_Info>");
				}
				if (number ==2) {
					msg.persistence = MQConstants.MQPER_PERSISTENT;
					msg.writeString("<Body><DeclarationOfficeID>0908�͹��Ŀ���dsl123424dsafdslkfkjkldf</DeclarationOfficeID><UnloadingLocation><ID>09060000001</ID><ArrivalDate>20110519000000328</ArrivalDate>");
				}
				if (number ==3) {
					msg.persistence = MQConstants.MQPER_NOT_PERSISTENT;
					msg.writeString("</UnloadingLocation><TransportEquipment><EquipmentIdentification><ID>CAIU2040426</ID></EquipmentIdentification><CharacteristicCode></CharacteristicCode><FullnessCode></FullnessCode></TransportEquipment></Body></Manifest>");
				}*/
				
				//String s = "���ã�����05��05��19ʱ������Ƿ��11.13Ԫ�����öȼ������ꡣ�������ƶ��ֻ�Ӫҵ����ÿ��28�ս�����9��(10������),��������9.8�ۣ��� ah.10086.cn/dt/khd";
				msg.persistence = MQConstants.MQPER_PERSISTENT;
				String s = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
				buffer = s.getBytes();
				
				//msg.writeString("AAA");

				msg.write(buffer);
				msg.replyToQueueManagerName = "TT"; 
				msg.replyToQueueName = "TT";
				queue.put(msg, pmo);
				
				Date dt=new Date();
				DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
				System.out.println(">>> "+ df.format(dt) +" Put "+ number +" message OK --- ");
				msg.clearMessage();	
				number++;
				Thread.sleep(100000);
			}
			System.out.println(">>> Put Message OK!!!");
		} catch (Exception ex) {
			ex.printStackTrace(System.out);
		}
	}

	public void runPutMsg() {
		try {
			MQStart();
			sendMsgToQueue();
			MQStop();
		} catch (Exception e) {
			e.printStackTrace(System.out);
			MQStop();
			System.exit(2);
		}
	}
}